package com.minivision.sdktestdemo.ui.view;

import android.app.Activity;
import android.inputmethodservice.KeyboardView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.event.PlayMusicEvent;

/**
 * Created by Administrator on 2016/6/6.
 */
public class LockPopupView {
    private PopupView mPopupView;
    private LockContentView mContentView;

    public LockPopupView(Activity context, PopContentView v) {
        mPopupView = new PopupView(context, v);
        mContentView = (LockContentView) v;
        mPopupView.setTouchListener(SLIDE);
        mPopupView.getPopupWindow().setFocusable(false);
        mPopupView.getPopupWindow().setBackgroundDrawable(null);
        mPopupView.getPopupWindow().setOutsideTouchable(false);
        mPopupView.getPopupWindow().setAnimationStyle(R.style.lockStyle);
    }

    private final View.OnTouchListener SLIDE = new View.OnTouchListener() {
        private float x;
        private float bx;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    int offX = (int) (event.getRawX() - x);
                    v.setTranslationX(bx + offX);
                    break;
                case MotionEvent.ACTION_DOWN:
                    bx = v.getTranslationX();
                    x = event.getRawX();
                    break;
                case MotionEvent.ACTION_UP:
                    int pos = (int) v.getTranslationX();
                    if (pos * 2 > v.getWidth()) {
                        mPopupView.getPopupWindow().dismiss();
                        mPopupView.getContext().finish();
                    } else {
                        v.animate().translationX(bx).setDuration(300).start();
                    }
                    break;
            }
            return true;
        }
    };

    public interface LockContentView {
        void updateEvent(PlayMusicEvent event);

        void startShowTime();

        void release();
    }

    public void show() {
        mPopupView.show(R.id.layout_main);
        mPopupView.getContext().findViewById(R.id.layout_main).post(new Runnable() {
            @Override
            public void run() {
                startShowTime();
            }
        });
//        PopupWindow popupWindow = mPopupView.getPopupWindow();
//        View v =  mPopupView.getContext().findViewById(R.id.layout_main);
//        popupWindow.showAtLocation(v, Gravity.RIGHT, 0, 0);
//        mPopupView.startUpdateBackAlpha(true);
    }

    private void startShowTime() {
        mContentView.startShowTime();
    }

    public void updateEvent(PlayMusicEvent event) {
        mContentView.updateEvent(event);
    }

    public void release() {
        mContentView.release();
        //mPopupView.getPopupWindow().dismiss();
    }
}
